/** 个人信息开始 **/
// 生日下拉列表 
$(function(){
    $("#date").selectDate()
    
    $("#days").focusout(function(){
        var year = $("#year option:selected").html()
        var month = $("#month option:selected").html()
        var day = $("#days option:selected").html()
        console.log(year+month+day)
        
    })
})

// 性别点击
var radio=$(".uitem-radiobox i");
radio.click(e=>{
    e=window.event;
    target=$(e.srcElement);
    if(target.attr("class")!="ico-center ico-checked"){
        for(var i in radio){
            radio.removeClass("ico-checked");
            $(".uitem-radio input").val()
        }
        target.addClass("ico-checked");
        var gender=target.parent().children()[1].innerHTML;
        $(".uitem-radio input").val(gender);
    }
})




if(sessionStorage.uname){
    $(".unlogin").hide();
    $(".login ").show();

    // 保存提交
    $(".uitem-actbox .btn-uitem-save").click(() => {
        $("#person").submit();
        return false;
    })

    // 传送用户名
    var somebody=sessionStorage.uname;

    // 获取页面元素
    var birth=$(".ct-userform .birth")[0];
    var phone=$(".ct-userform .phone").children()[1].children[0];
    var email=$(".ct-userform .email").children()[1].children[0];

    // 渲染页面
    $.get("../php/jc_person.php","hide=getUser",data=>{
        data.forEach(obj=>{
            if(obj.name==sessionStorage.uname){
                console.log(obj);
                $($(".username>div>input")).val(obj.name);
                $(phone).val(obj.phone)
                $(email).val(obj.email);
                var inp=document.createElement("input");
                inp.type="hidden";
                inp.name="id";
                inp.value=obj.id;
                person.appendChild(inp);
                if(obj.gender == ""){
                    $(".ico-center:eq(0)").removeClass("ico-checked");   
                }else if(obj.gender == "男"){
                    $(".ico-center:eq(0)").addClass("ico-checked");
                }else if(obj.gender == "女"){
                    $(".ico-center:eq(0)").removeClass("ico-checked");
                    $(".ico-center:eq(1)").addClass("ico-checked");
                }
                if(obj.birthday == ""){
                    $(".birth p").html("未填写你的生日");
                }else{
                    $(".birth p").html("你的生日为"+obj.birthday).css("color","green");
                }
                $(".hid4").attr("name", "uid").val(obj.id);
            }
            
        })
    },"JSON")
}else{
    $(".unlogin").show();
    $(".login ").hide();
}
//提交个人信息
person.onsubmit= () =>{
    let gender = $(".ico-checked").next().html(),
    year = $("#year").find("option:selected").text(),
    month = $("#month").find("option:selected").text(),
    date = $("#days").find("option:selected").text(),
    val = $('input[name="utx"]:checked').val(),
    img = "",
    birthday = year + month + date;
    if(year == "选择年份" || month == "选择月份" || date == "选择日期"){
        $(".birth>p").html("请填写完整的生日").css("color","red");
        return false;
    }
    if(val){
        if(val == 0){
            img = "touxiang.png";
        }else if(val == 1){
            img = "touxiang1.jpeg";
        }else if(val == 2){
            img = "touxiang2.jpg";
        }else if(val == 3){
            img = "touxiang3.jpg";
        }else if(val == 4){
            img = "touxiang4.jpg";
        }else if(val == 5){
            img = "touxiang5.jpg";
        } 
    }else{
        return false;
    }
    $(".hid1").attr("name", "img").val(img);
    $(".hid2").attr("name", "birth").val(birthday);
    $(".hid3").attr("name", "gender").val(gender);
    sessionStorage.clear();
}